package com.qilin.hscs.repository;

import com.qilin.hscs.pojo.Host;
import com.qilin.hscs.pojo.User;
import com.qilin.hscs.pojo.UserHost;
import com.qilin.hscs.vo.UserHostVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserHostRepository extends JpaRepository<UserHost,Integer> {

    @Query(value = "select uh.* from u_user_host uh join u_user u on uh.user_id=u.id join u_host h on uh.host_id=h.id where u.phone=?1 and h.mac=?2",nativeQuery = true)
    UserHost findByPhoneAndMac(String  phone, String mac);

    @Query(value = "select new com.qilin.hscs.vo.UserHostVO(uh.role,u.name,u.phone) from UserHost uh,User u where uh.userId=u.id and uh.hostId=?1")
    List<UserHostVO> findUserByHostId(Integer hostId);

    @Query(value = "select new com.qilin.hscs.vo.UserHostVO(h.mac,h.name,uh.isUse,uh.role) from UserHost uh,Host h where uh.hostId=h.id and uh.userId=?1")
    List<UserHostVO> findHostByUserId(Integer userId);

    List<UserHost> findUserHostByUserId(Integer userId);
}
